package com.yzy.soft.system.freemarker;

//import com.evada.inno.core.exception.BusinessException;
import com.yzy.soft.CodeBuilderRestApplication;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.NullCacheStorage;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import java.io.IOException;

/**
 * Created by Ay on 2016/7/27.
 */
public class FreeMarkerTemplateUtils {

    public FreeMarkerTemplateUtils(){}
    private static final Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_23);

    static{
        //这里比较重要，用来指定加载模板所在的路径templates  com.yzy.ftl
//        CONFIGURATION.setTemplateLoader(new ClassTemplateLoader(FreeMarkerTemplateUtils.class, "/com/sinosoft/liscw/system/freemarker/ftl/"));
        CONFIGURATION.setTemplateLoader(new ClassTemplateLoader(CodeBuilderRestApplication.class, "/WEB-INF/ftl"));


       // CONFIGURATION.setTemplateLoader(new FileTemplateLoader(new File("项目根路径")););

        CONFIGURATION.setDefaultEncoding("UTF-8");
        CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        CONFIGURATION.setCacheStorage(NullCacheStorage.INSTANCE);
    } 

    public static Template getTemplate(String templateName) throws IOException {
        try {
            return CONFIGURATION.getTemplate(templateName);
        } catch (IOException e) {
            throw e;
        }
    }

    public static void clearCache() {
        CONFIGURATION.clearTemplateCache();
    }
}
 
